﻿using System.IO;
using System.IO.Ports;
using System.Linq;

namespace Eric.SerialPorts
{
    public class SerialPortParameter : ISerialPortParameter
    {
        #region Private Properties
        private readonly string _portName;
        private readonly int _baudRate;
        private readonly Parity _parity;
        private readonly int _dataBits;
        private readonly StopBits _stopBits;
        private readonly int _readTimeOut;
        private readonly int _writeTimeOut;
        #endregion

        #region ISerialPortParameter
        public string PortName { get { return _portName; } }
        public int BaudRate { get { return _baudRate; } }
        public Parity Parity { get { return _parity; } }
        public int DataBits { get { return _dataBits; } }
        public StopBits StopBits { get { return _stopBits; } }
        public int ReadTimeOut { get { return _readTimeOut; } }
        public int WriteTimeOut { get { return _writeTimeOut; } }
        #endregion

        #region Constructors
        /// <summary>
        ///     Initializes a new instance of the DIH.SerialPorts.SerialPortParameter class using
        ///     the specified port name, baud rate, parity bit.
        /// </summary>
        /// <param name="portName">
        ///   The port to use (for example, COM1).
        /// </param>
        /// <exception >
        ///   System.IO.IOException:
        ///      The specified port could not be found.
        /// </exception>
        public SerialPortParameter(string portName)
            : this(portName, 9600, Parity.None)
        {

        }

        /// <summary>
        ///     Initializes a new instance of the DIH.SerialPorts.SerialPortParameter class using
        ///     the specified port name, baud rate, parity bit.
        /// </summary>
        /// <param name="portName">
        ///   The port to use (for example, COM1).
        /// </param>
        /// <param name="baudRate">
        ///    The baud rate to use.
        /// </param>
        /// <param name="parity">
        /// The parity value.
        /// </param>
        /// <exception >
        ///   System.IO.IOException:
        ///      The specified port could not be found.
        /// </exception>
        public SerialPortParameter(string portName, int baudRate, Parity parity)
            : this(portName, baudRate, parity, 8, StopBits.One, -1, -1)
        {

        }

        /// <summary>
        ///     Initializes a new instance of the DIH.SerialPorts.SerialPortParameter class using
        ///     the specified port name, baud rate, parity bit, data bits, stop bit, read time out, and write time out.
        /// </summary>
        /// <param name="portName">
        ///   The port to use (for example, COM1).
        /// </param>
        /// <param name="baudRate">
        ///    The baud rate to use.
        /// </param>
        /// <param name="parity">
        /// The parity value.
        /// </param>
        /// <param name="dataBits">
        /// The data bit value.
        /// </param>
        /// <param name="stopBits">
        ///   One of the System.IO.Ports.SerialPort.StopBits values.
        /// </param>
        /// <param name="readTimeOut">
        ///   The read time out value.
        /// </param>
        /// <param name="writeTimeOut">
        ///   The read time out value.
        /// </param>
        /// <exception >
        ///   System.IO.IOException:
        ///      The specified port could not be found.
        /// </exception>
        public SerialPortParameter(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, int readTimeOut, int writeTimeOut)
        {
            if (!(SerialPort.GetPortNames().Any(t => t == portName)))
                throw new IOException(string.Format("The specified port name:\"{0}\" could not be found.", portName));
            _portName = portName;
            _baudRate = baudRate;
            _parity = parity;
            _dataBits = dataBits;
            _stopBits = stopBits;
            _readTimeOut = readTimeOut;
            _writeTimeOut = writeTimeOut;
        }
        #endregion
    }
}
